package cn.dengqiang.pet.controller;

import cn.dengqiang.basic.util.AjaxResult;
import cn.dengqiang.basic.util.LoginContext;
import cn.dengqiang.basic.util.PageList;
import cn.dengqiang.pet.domain.Pet;
import cn.dengqiang.pet.domain.PetDetail;
import cn.dengqiang.pet.query.PetQuery;
import cn.dengqiang.pet.service.IPetService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @PutMapping
    public AjaxResult save(@RequestBody Pet pet){
        try {
            if (pet.getId()==null){
                petService.add(pet);
            }else {
                petService.update(pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存失败"+e.getMessage());
        }
    }




    @ApiOperation(value = "删除",notes = "根据id删除一条数据")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            petService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败!"+e.getMessage());
        }
    }

    @ApiOperation(value = "查询",notes = "根据id查询一条数据")
    @GetMapping("/{id}") //根据id查询
    public Pet getById(@PathVariable("id") Long id){
        Pet pet = petService.getById(id);
        System.out.println(pet);
        return pet;
    }

    @ApiOperation(value = "查询",notes = "查询所有信息")
    @GetMapping //pet 查询所有
    public List<Pet> getAll(){
        return petService.getAll();
    }

    @ApiOperation(value = "查询",notes = "分页查询")
    @PostMapping("/list")//分页查询
    public PageList<Pet> query(@RequestBody PetQuery query){
        System.out.println("我进来了");
        query.setState(1);
        return petService.queryPage(query);
    }
    @ApiOperation(value = "查询",notes = "分页查询")
    @PostMapping//分页查询
    public PageList<Pet> list(@RequestBody PetQuery query){
        System.out.println("我进来了");
        return petService.queryPage(query);
    }

    //获取一个 get
    @GetMapping("/detail/{petId}") //Get http://localhost:8080/deparment/1 deparment/2
    public PetDetail getDetailByPetId(@PathVariable("petId") Long petId){
        return petService.getDetailByPetId(petId);
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onOrOffsale(ids,1);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上架失败"+e.getMessage());
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.onOrOffsale(ids,0);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上架失败"+e.getMessage());
        }

    }
    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long id, HttpServletRequest request){
        Long loginInfoId = LoginContext.getLoginInfo(request).getId();

        try {
            petService.adopt(id,loginInfoId); //1 表示上架，0表示下架
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("包养失败！"+e.getMessage());
        }
    }
}
